package serveur;

import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.CommonFile;
import common.ReceiveFile;
import common.SendFile;

public class AnnonceurServerDelegate implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(MainServer.class.getName());
	private SendFile m_sendFile;
	private ReceiveFile m_recvFile;
	private Socket m_dataSocket;
	private boolean m_bFini;
	private int m_TransfertMode;
		


	public AnnonceurServerDelegate(Socket sock) {
		// TODO Auto-generated constructor stub
		this.m_dataSocket = sock;
		this.m_bFini = false;
		this.m_TransfertMode = CommonFile.NoTransfertMode;
	}
	
	public void setM_sendFile(SendFile mSendFile) {
		m_sendFile = mSendFile;
	}
	
	public int getM_TransfertMode() {
		return m_TransfertMode;
	}

	public void setM_TransfertMode(int mTransfertMode) {
		m_TransfertMode = mTransfertMode;
	}

	public Socket getM_dataSocket() {
		return m_dataSocket;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Run");
		logger.log(Level.INFO,"***** Run AnnonceurServerDelegate *****");
		//while ( m_bFini == false ){
			switch( m_TransfertMode ){
			case CommonFile.SendTranfertMode:
				try {
					System.out.println("CommonFile.SendTransfertMode");
					m_sendFile.envoieFichier();
					m_TransfertMode = CommonFile.NoTransfertMode;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case CommonFile.ReceiveTransfertMode:
				try {
					System.out.println("CommonFile.ReceiveTransfertMode");
					m_recvFile.recoitFichier();
					m_TransfertMode = CommonFile.NoTransfertMode;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case CommonFile.NoTransfertMode:
			default:
				try {
					System.out.println("CommonFile.NoTransfertMode");					
					Thread.sleep(50);
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
		
		//}
//			try {
//				m_dataSocket.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
	}

	public void setM_recvFile(ReceiveFile mRecvFile) {
		m_recvFile = mRecvFile;
	}
	
}
